package ace.module.oauth2.server.core.impl.authorization.token;

import java.time.Instant;
import java.util.*;
import org.springframework.lang.Nullable;
import org.springframework.security.crypto.keygen.Base64StringKeyGenerator;
import org.springframework.security.crypto.keygen.StringKeyGenerator;
import org.springframework.security.oauth2.core.ClaimAccessor;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.security.oauth2.core.oidc.OidcScopes;
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat;
import org.springframework.security.oauth2.server.authorization.token.*;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author caspar
 * @date 2023/3/7 10:41 根据token格式类型 {@link OAuth2TokenFormat} 生成对应的 token value格式 {@link Oauth2StringKeyUUIDGenerator} 与 {@link Base64StringKeyGenerator}
 */
public class Oauth2AccessTokenGenerator implements OAuth2TokenGenerator<OAuth2AccessToken> {
  private final StringKeyGenerator accessTokenGenerator = new Oauth2StringKeyUUIDGenerator();
  private final StringKeyGenerator accessTokenBase64Generator =
      new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 96);

  private OAuth2TokenCustomizer<OAuth2TokenClaimsContext> accessTokenCustomizer =
      context -> {
        if (context.getAuthorization() == null
            || !context.getAuthorization().getAuthorizedScopes().contains(OidcScopes.OPENID)) {
          return;
        }
        if (context.getAuthorization().getAttributes() == null
            || context.getAuthorization().getAttributes().size() == 0) {
          return;
        }
        context
            .getAuthorization()
            .getAttributes()
            .entrySet()
            .forEach(
                p -> {
                  context.getClaims().claim(p.getKey(), p.getValue());
                });
      };

  @Nullable
  @Override
  public OAuth2AccessToken generate(OAuth2TokenContext context) {
    if (!OAuth2TokenType.ACCESS_TOKEN.equals(context.getTokenType())
        || !OAuth2TokenFormat.REFERENCE.equals(
            context.getRegisteredClient().getTokenSettings().getAccessTokenFormat())) {
      return null;
    }

    String issuer = null;
    if (context.getAuthorizationServerContext() != null) {
      issuer = context.getAuthorizationServerContext().getIssuer();
    }
    RegisteredClient registeredClient = context.getRegisteredClient();

    Instant issuedAt = Instant.now();
    Instant expiresAt =
        issuedAt.plus(registeredClient.getTokenSettings().getAccessTokenTimeToLive());

    // @formatter:off
    OAuth2TokenClaimsSet.Builder claimsBuilder = OAuth2TokenClaimsSet.builder();
    if (StringUtils.hasText(issuer)) {
      claimsBuilder.issuer(issuer);
    }
    claimsBuilder
        .subject(context.getPrincipal().getName())
        .audience(Collections.singletonList(registeredClient.getClientId()))
        .issuedAt(issuedAt)
        .expiresAt(expiresAt)
        .notBefore(issuedAt)
        .id(UUID.randomUUID().toString());
    if (!CollectionUtils.isEmpty(context.getAuthorizedScopes())) {
      claimsBuilder.claim(OAuth2ParameterNames.SCOPE, context.getAuthorizedScopes());
    }

    // @formatter:on

    if (this.accessTokenCustomizer != null) {
      // @formatter:off
      OAuth2TokenClaimsContext.Builder accessTokenContextBuilder =
          OAuth2TokenClaimsContext.with(claimsBuilder)
              .registeredClient(context.getRegisteredClient())
              .principal(context.getPrincipal())
              .authorizationServerContext(context.getAuthorizationServerContext())
              .authorizedScopes(context.getAuthorizedScopes())
              .tokenType(context.getTokenType())
              .authorizationGrantType(context.getAuthorizationGrantType());
      if (context.getAuthorization() != null) {
        accessTokenContextBuilder.authorization(context.getAuthorization());
      }
      if (context.getAuthorizationGrant() != null) {
        accessTokenContextBuilder.authorizationGrant(context.getAuthorizationGrant());
      }
      // @formatter:on

      OAuth2TokenClaimsContext accessTokenContext = accessTokenContextBuilder.build();
      this.accessTokenCustomizer.customize(accessTokenContext);
    }

    OAuth2TokenClaimsSet accessTokenClaimsSet = claimsBuilder.build();

    String accessTokenValue = this.generateAccessTokenValue(context);

    OAuth2AccessToken accessToken =
        new OAuth2AccessTokenClaims(
            OAuth2AccessToken.TokenType.BEARER,
            accessTokenValue,
            accessTokenClaimsSet.getIssuedAt(),
            accessTokenClaimsSet.getExpiresAt(),
            context.getAuthorizedScopes(),
            accessTokenClaimsSet.getClaims());

    return accessToken;
  }

  private String generateAccessTokenValue(OAuth2TokenContext context) {
    if (OAuth2TokenFormat.SELF_CONTAINED.equals(
        context.getRegisteredClient().getTokenSettings().getAccessTokenFormat())) {
      return this.accessTokenBase64Generator.generateKey();
    } else {
      return this.accessTokenGenerator.generateKey();
    }
  }

  /**
   * Sets the {@link OAuth2TokenCustomizer} that customizes the
   * {@link OAuth2TokenClaimsContext#getClaims() claims} for the {@link OAuth2AccessToken}.
   *
   * @param accessTokenCustomizer the {@link OAuth2TokenCustomizer} that customizes the claims for the {@code OAuth2AccessToken}
   */
  public void setAccessTokenCustomizer(
      OAuth2TokenCustomizer<OAuth2TokenClaimsContext> accessTokenCustomizer) {
    Assert.notNull(accessTokenCustomizer, "accessTokenCustomizer cannot be null");
    this.accessTokenCustomizer = accessTokenCustomizer;
  }

  private static final class OAuth2AccessTokenClaims extends OAuth2AccessToken
      implements ClaimAccessor {
    private final Map<String, Object> claims;

    private OAuth2AccessTokenClaims(
        TokenType tokenType,
        String tokenValue,
        Instant issuedAt,
        Instant expiresAt,
        Set<String> scopes,
        Map<String, Object> claims) {
      super(tokenType, tokenValue, issuedAt, expiresAt, scopes);
      this.claims = claims;
    }

    @Override
    public Map<String, Object> getClaims() {
      return this.claims;
    }
  }
}
